package com.cn.xiaonuo.slbfinances.slborders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slborders.entity.ExpiredError;
import com.cn.xiaonuo.slbfinances.slborders.mapper.ExpiredErrorMapper;
import com.cn.xiaonuo.slbfinances.slborders.param.ExpiredErrorParam;
import com.cn.xiaonuo.slbfinances.slborders.service.ExpiredErrorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: slb-reports-java
 * @Author: likui
 * @CreateTime: 2022-05-25  13:18
 * @Description: TODO
 */
@Service
public class ExpiredErrorServiceImpl implements ExpiredErrorService {

    @Autowired
    private ExpiredErrorMapper expiredErrorMapper;

    @Override
    @DataSource(name = "slb-gwq")
    public BusinessMessage expiredErrorList(ExpiredErrorParam expiredErrorParam) {
        BusinessMessage message = new BusinessMessage();

        try {
            Integer pageNo = expiredErrorParam.getPageNo();
            Integer pageSize = expiredErrorParam.getPageSize();
            if (null == pageNo || pageNo < 1) {
                pageNo = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            // 设置分页参数
            PageHelper.startPage(pageNo, pageSize);
            LambdaQueryWrapper<ExpiredError> wrapper = new LambdaQueryWrapper<>();
            if (expiredErrorParam.getBeginTime() != null && expiredErrorParam.getBeginTime() != "" && expiredErrorParam.getEndTime() != null && expiredErrorParam.getEndTime() != "") {
                wrapper.between(ExpiredError::getCreateTime, expiredErrorParam.getBeginTime(), expiredErrorParam.getEndTime());
            }
            if (expiredErrorParam.getType() != null) {
                wrapper.eq(ExpiredError::getType, expiredErrorParam.getType());
            }
            if (expiredErrorParam.getOrderId() != null && expiredErrorParam.getOrderId() != "") {
                wrapper.eq(ExpiredError::getOrderId, expiredErrorParam.getOrderId());
            }
            if (expiredErrorParam.getPhone() != null && expiredErrorParam.getPhone() != "") {
                wrapper.eq(ExpiredError::getPhone, expiredErrorParam.getPhone());
            }
            if (expiredErrorParam.getStatus() != null && expiredErrorParam.getStatus() != 0) {
                wrapper.eq(ExpiredError::getStatus, expiredErrorParam.getStatus());
            }
            List<ExpiredError> expiredErrors = expiredErrorMapper.selectList(wrapper);
            PageInfo<ExpiredError> pageInfo = new PageInfo<>(expiredErrors);
            message.setMessage(Status.success);
            message.setData(pageInfo);
            message.setSuccess(true);
            message.setCode(Status.successCode);
        } catch (Exception e) {
            message.setMessage(Status.fail);
            message.setSuccess(false);
            message.setCode(Status.failCode);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @DataSource(name = "slb-gwq")
    public BusinessMessage exceptionHandling(ExpiredErrorParam expiredErrorParam) {
        BusinessMessage message = new BusinessMessage();
        if (expiredErrorParam.getId()==null){
            message.setMessage("异常信息ID不能为空");
            message.setSuccess(false);
            message.setCode(Status.failCode);
            return message;
        }
        try {
            for (Integer id:expiredErrorParam.getId()){
                ExpiredError expiredError = expiredErrorMapper.selectById(id);
                if (expiredError==null){
                    message.setMessage("该笔异常信息不存在");
                    message.setSuccess(false);
                    message.setCode(Status.failCode);
                    return message;
                }
                if (expiredError.getStatus()==2){
                    message.setMessage(Status.success);
                    message.setSuccess(true);
                    message.setCode(Status.successCode);
                    return message;
                }
                expiredError.setStatus(2);
                expiredErrorMapper.updateById(expiredError);
            }
            message.setMessage(Status.success);
            message.setSuccess(true);
            message.setCode(Status.successCode);
        }catch (Exception e){
            message.setMessage(Status.fail);
            message.setSuccess(false);
            message.setCode(Status.failCode);
            e.printStackTrace();
        }
        return message;
    }
}
